$(function () {
    let userId = getQueryString("userId");
    let url = '../version/list';
    if (userId) {
        url += '?userId=' + userId;
    }
    $("#jqGrid").Grid({
        url: url,
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '下载地址', name: 'apkAddr', index: 'apkAddr', width: 80},
            // {label: 'sessionId', name: 'sessionId', index: 'session_id', width: 80 },
            {
                label: '是否强制更新', name: 'mustUpdate', index: 'mustUpdate', width: 15, formatter: function (value) {
                    if (value == 0) {
                        return '<span class="label label-info">不强制更新</span>';
                    } else if (value == 1) {
                        return '<span class="label label-success">弹框提示</span>';
                    } else {
                        return '<span class="label label-success">强制更新</span>';
                    }
                }
            },
            {label: '版本号', name: 'versionCode', index: 'versionCode', width: 20},
            {label: '版本名称', name: 'versionName', index: 'versionName', width: 20},
            {
                label: '版本类型', name: 'type', index: 'type', width: 20, formatter: function (value) {
                    if (value == 0) {
                        return '<span class="label label-info">安卓版</span>';
                    } else if (value == 1) {
                        return '<span class="label label-success">苹果版</span>';
                    }
                }
            },
            {label: '更新提示', name: 'msg', index: 'msg', width: 60},
            {label: '加密串', name: 'fileMd5', index: 'fileMd5', width: 25},
            {
                label: '创建日期', name: 'createDate', index: 'createDate', width: 25, formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd hh:mm:ss")
                }
            }
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        version: {},
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.version = {};
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.version.id == null ? "../version/save" : "../version/update";
            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.version),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../version/delete",
                    contentType: "application/json",
                    params: JSON.stringify(ids),
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../version/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.version = r.version;
                }
            });
        },

        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
        }
        , handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleSuccessPicUrl: function (res, file) {
            vm.version.apkAddr = file.response.url;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        eyeImagePicUrl: function () {
            var url = vm.version.apkAddr;
            eyeImage(url);
        },

        eyeImage: function (e) {
            eyeImage($(e.target).attr('src'));
        }
    }
});